/**
 * 
 */
package org.traffometer.common.data;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.traffometer.opendmtp.protocol.GPSException;
import org.traffometer.opendmtp.protocol.GeoPoint;

/**
 * Tests for TraffoPacket.
 * 
 * @author Alexey Olkov
 * 
 */
public class TestTraffoPacket {

  /**
   * Tests packet creation.
   * 
   * @throws GPSException if the GeoEvent data are invalid.
   */
  @Test
  public void testPacketCreation() throws GPSException {
    double lat1, lon1, lat2, lon2;
    long time1, time2;
    String vehicle = "School_bus12yguygjhg";
    lat1 = 10;
    lon1 = 32.44;
    time1 = 56778;
    lat2 = 33.45;
    lon2 = 170.23;
    time2 = 88883;
    GeoPoint point1 = new GeoPoint(lat1, lon1, time1);
    GeoPoint point2 = new GeoPoint(lat2, lon2, time2);

    TraffoPacket pkt = new TraffoPacket(point1, point2, vehicle);

    assertEquals("Checks 1st lat", pkt.getLatitude1(), lat1);
    assertEquals("Checks 1st lon", pkt.getLongitude1(), lon1);
    assertEquals("Checks 1st time", pkt.getTimeStamp1(), time1);

    assertEquals("Checks 2nd lat", pkt.getLatitude2(), lat2);
    assertEquals("Checks 2nd lon", pkt.getLongitude2(), lon2);
    assertEquals("Checks 2nd time", pkt.getTimeStamp2(), time2);

    assertEquals("Checks Vehicle type", pkt.getVehicleType(), vehicle);

    assertEquals("Checks toString",
        "( 10.0 , 32.44 at 56778) - ( 33.45 , 170.23 at 88883): School_bus12yguygjhg", pkt
            .toString());
    
    byte[] bytes = null;
//    bytes = pkt.getPacket().encode();
//
//    System.out.println("Hex bytes from getPacket().encode()");
//    for (byte foo : bytes) {
//      System.out.printf("%x ", foo);
//    }
//    System.out.println();
//    System.out.println("Java signed bytes from getPacket().encode()");
//    for (byte foo : bytes) {
//      System.out.printf("%d ", foo);
//    }
//    System.out.println();
//    System.out.println("unsigned bytes from getPacket().encode()");
//    for (byte foo : bytes) {
//      System.out.printf("%d ", foo+128);
//    }
//    System.out.println();
    
    bytes = pkt.getPacketBytes();
    
//    System.out.println("Hex bytes from getPacketBytes()");
//    for (byte foo : bytes) {
//      System.out.printf("%x ", foo);
//    }
//    System.out.println();
//    System.out.println("Java signed bytes from getPacketBytes()");
//    for (byte foo : bytes) {
//      System.out.printf("%d ", foo);
//    }
//    System.out.println();
//    System.out.println("unsigned bytes from getPacketBytes()");
//    for (byte foo : bytes) {
//      System.out.printf("%d ", foo+128);
//    }
//    System.out.println();
    
    assertNotNull("Check if something has been written into byte array",bytes);
    
    TraffoPacket res = new TraffoPacket(bytes);
    assertEquals("Checks 1st lat", pkt.getLatitude1(), res.getLatitude1(),0.000001);
    assertEquals("Checks 1st lon", pkt.getLongitude1(),res.getLongitude1(),0.000001);
    assertEquals("Checks 1st time", pkt.getTimeStamp1(), res.getTimeStamp1());

    assertEquals("Checks 2nd lat", pkt.getLatitude2(),res.getLatitude2(),0.000001);
    assertEquals("Checks 2nd lon", pkt.getLongitude2(),res.getLongitude2(),0.000001);
    assertEquals("Checks 2nd time", pkt.getTimeStamp2(), res.getTimeStamp2());

    assertEquals("Checks Vehicle type", pkt.getVehicleType(), res.getVehicleType());

    
    TraffoPacket res2 = new TraffoPacket(pkt.getPacket());
    assertEquals("Checks 1st lat", Math.round(pkt.getLatitude1()), Math.round(res2.getLatitude1()));
    assertEquals(
      "Checks 1st lon", 
      Math.round(pkt.getLongitude1()),
      Math.round(res2.getLongitude1())
    );
    assertEquals("Checks 1st time", pkt.getTimeStamp1(), res2.getTimeStamp1());

    assertEquals("Checks 2nd lat", Math.round(pkt.getLatitude2()), Math.round(res2.getLatitude2()));
    assertEquals(
      "Checks 2nd lon", 
      Math.round(pkt.getLongitude2()), 
      Math.round(res2.getLongitude2())
    );
    assertEquals("Checks 2nd time", pkt.getTimeStamp2(), res2.getTimeStamp2());

    assertEquals("Checks Vehicle type", pkt.getVehicleType(), res2.getVehicleType());

    
    
  }

  /**
   * Tests invalid data in GeoEvent.
   * 
   */
  @Test
  public void testInvalidPacketCreation() {
    double lat1, lon1, lat2, lon2;
    long time1, time2;
    String vehicle = "School_bus12yguygjhg";
    lat1 = 10;
    lon1 = 3234.44;
    time1 = -56778;
    lat2 = 2;
    lon2 = 0;
    time2 = 88883;
    GeoPoint point1 = new GeoPoint(lat1, lon1, time1);
    GeoPoint point2 = new GeoPoint(lat2, lon2, time2);
    try {
      new TraffoPacket(point1, point2, vehicle);
      fail("No Exception thrown");
    }
    catch (GPSException e) {
      return;
    }
  }
}
